﻿namespace Sidvall.Security
{
    public class IdentityValidationResultCollection : System.Collections.ObjectModel.Collection<IdentityValidationResult>
    {
        #region Public Members

        #region Create

        public static IdentityValidationResultCollection Create(int count)
        {
            IdentityValidationResultCollection items;

            items = new IdentityValidationResultCollection();
            for (int i = 0; i < count; i++)
                items.Add(null);
            return items;
        }

        #endregion
        #region GetResult

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
        public IdentityValidationResult GetResult()
        {
            var item = new IdentityValidationResult()
            {
                IdentityStatus = Sidvall.Security.IdentityStatus.Valid,
                Claims = new Sidvall.Security.ClaimCollection(),
                ObjectParameters = new Sidvall.Data.ObjectParameterCollection(),
            };
            foreach (var result in this)
            {
                if (result == null)
                    continue;
                if ((int)result.IdentityStatus > (int)item.IdentityStatus)
                {
                    item.IdentityStatus = result.IdentityStatus;
                    item.ReasonPhrase = result.ReasonPhrase;
                }
                item.Claims.Merge(result.Claims);
                item.ObjectParameters.Merge(result.ObjectParameters);
            }
            return item;
        }

        #endregion

        #endregion
        #region Constructors

        public IdentityValidationResultCollection()
        {
        }

        #endregion
    }
}
